package club.bigtian.mf.plugin.action.flex;

import club.bigtian.mf.plugin.core.util.ProjectUtils;
import club.bigtian.mf.plugin.core.util.TableUtils;
import club.bigtian.mf.plugin.windows.MybatisFlexCodeGenerateDialog;
import cn.hutool.core.util.ObjUtil;
import com.intellij.database.model.DasTable;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import javax.swing.SwingUtilities;
import org.jetbrains.annotations.NotNull;

/**
 * 表动作
 *
 * @author daijunxiong
 * @date 2023/06/22
 */
public class TableAction extends AnAction {


  @Override
  public void actionPerformed(AnActionEvent e) {

    SwingUtilities.invokeLater(() -> {
      MybatisFlexCodeGenerateDialog generateWin = new MybatisFlexCodeGenerateDialog(e);
      generateWin.setVisible(true);
    });

  }

  /**
   * 判断选中的是否是表，是表则显示，否则不显示
   *
   * @param event 事件
   */
  @Override
  public void update(AnActionEvent event) {
    ProjectUtils.setCurrentProject(event.getProject());
    DasTable dasTable = TableUtils.getSelectedSingleTable(event);
    event.getPresentation().setVisible(ObjUtil.isNotNull(dasTable));
  }

  @Override
  public @NotNull ActionUpdateThread getActionUpdateThread() {
    return ActionUpdateThread.BGT;
  }


}